package com.example.fist.control;

import com.example.fist.dao.UserDao;
import com.example.fist.entity.AdminEntity;
import com.example.fist.personRepository.AdminJpaRepository;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 登录相关的
 */
@RestController
@CrossOrigin(allowCredentials = "true")
public class Login {
    @Autowired
    private AdminJpaRepository userRepository;

    /**
     * 登录
     *
     * @param name
     * @param pass
     * @param session
     * @return
     */
    @RequestMapping("/login/login")
    String login(String name, String pass, HttpSession session) {
        UserDao dao = new UserDao(userRepository);
        AdminEntity ad = dao.Login(name, pass);

        JSONObject jon = new JSONObject();
        if (ad == null) {
            try {
                jon.put("code", 1001);
                jon.put("messg", "用户名不存在或密码错误");

            } catch (JSONException e) {
                e.printStackTrace();
            }
           return jon.toString();
        }
        session.setAttribute("User", ad);
        try {
            JSONObject jo = new JSONObject();
            jo.put("ID", ad.getAdId());
            jo.put("name", ad.getName());
            jon.put("data", jo);
            jon.put("code", 200);
            jon.put("messg", "Ok");

        } catch (JSONException e) {
            return "{" +
                   "messg: json数据组装出错," +
                 "code: 8001" +
                   "}";
        }

       return jon.toString();


    }

    /**
     * 是否登录
     */
    @RequestMapping("/login/islogin")
    public String islogin(HttpSession session) {
        AdminEntity ad = (AdminEntity) session.getAttribute("User");
        JSONObject jon = new JSONObject();
        if (ad == null) {
            try {
                jon.put("code", 10002);
                jon.put("messg", "用户未登录");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jon.toString();
        }

        JSONObject jo = new JSONObject();
        try {
            jo.put("ID", ad.getAdId());
            jo.put("name", ad.getName());
            jon.put("data", jo);
            jon.put("code", 200);
            jon.put("messg", "用户已登录");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jon.toString();
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @RequestMapping("/login/exit")
    public String ExitLogin(HttpSession session) {

        session.removeAttribute("User");
        JSONObject jon = new JSONObject();
        try {
            jon.put("code", 200);
            jon.put("messg", "退出成功");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jon.toString();
    }

}
